package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.MessageEnum;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.error.CourseException;
import com.softer.wxzj.common.flow.enums.FlowTypeEnum;
import com.softer.wxzj.common.flow.util.FlowAddAuditUtil;
import com.softer.wxzj.common.flow.util.FlowAuditUtil;
import com.softer.wxzj.entity.ExpPayReq;
import com.softer.wxzj.entity.FlowAuditdetail;
import com.softer.wxzj.mapper.ExpPayReqAuditMapper;
import com.softer.wxzj.mapper.ExpPayReqMapper;
import com.softer.wxzj.mapper.FlowAuditdetailMapper;
import com.softer.wxzj.service.ExpPayReqAuditService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Liu jinming
 * @date 2019/8/18
 */
@Service
public class ExpPayReqAuditServiceImpl implements ExpPayReqAuditService {
    @Autowired
    private FlowAddAuditUtil flowAddAuditUtil;
    @Autowired
    private ExpPayReqMapper expPayReqMapper;
    @Autowired
    private ExpPayReqAuditMapper expPayReqAuditMapper;
    @Autowired
    private FlowAuditdetailMapper flowAuditdetailMapper;
    @Autowired
    private FlowAuditUtil flowUtil;

    /**
     * 拨款审核
     *
     * @param businessId 审核ID exp_pay_req id
     * @param opinion    审批意见
     * @param mark       审批通过 audit_pass
     *                   审批否决 audit_veto
     *                   审批退回 audit_back
     * @return 操作结果
     */

    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    @Override
    public CommonResult audit(String businessId, String opinion, String mark, String proDate) {
        if (mark.equals(String.valueOf(NumberEnum.ZERO.getNum()))) {//不同意
            mark = "audit_veto";
        } else {
            mark = "audit_pass";//同意
        }
        //返回审批状态
        String auditState = flowAddAuditUtil.audit(businessId, opinion, mark,null);
        if (StringUtils.isBlank(auditState)) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
        }
        //修改exp_pay_req表的审核状态
        ExpPayReq expPayReq = new ExpPayReq();
        expPayReq.setId(businessId);
        if (auditState.equals(String.valueOf(NumberEnum.TWO.getNum()))) {
            //拨款终审通过
            expPayReq.setFlowState(String.valueOf(NumberEnum.TWO.getNum()));
            expPayReq.setStep(String.valueOf(NumberEnum.SIX.getNum()));
            expPayReq.setState(String.valueOf(NumberEnum.TWO.getNum()));
            expPayReq.setGrantcompletionDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        }
        if (auditState.equals(String.valueOf(NumberEnum.THREE.getNum()))) {
            //拨款审核不通过
            expPayReq.setFlowState(String.valueOf(NumberEnum.THREE.getNum()));
            expPayReq.setStep(String.valueOf(NumberEnum.SEVEN.getNum()));
            expPayReq.setState(String.valueOf(NumberEnum.THREE.getNum()));
            //expPayReqDetailMapper.deleteByPayReqId(businessId);
        }
        if(StringUtils.isNotEmpty(expPayReq.getFlowState())){
            int num = expPayReqMapper.updateByPrimaryKeySelective(expPayReq);
            if (num == NumberEnum.ZERO.getNum()) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
            }
        }
        return CommonResult.ResponseResult(NumberEnum.ONE.getNum());
    }

    /**
     * 拨款审核查询列表
     *
     * @param page  分页实体
     *              pageNum 页码
     *              pageSize 每页条数
     * @return 审核查询列表
     */
    @Override
    public PageInfo<ExpPayReq> getAuditSearchList(Page page, String fuzzy) {
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.PAY_REQ.getType());
        List<ExpPayReq> list = new ArrayList<>();
        if (null != map) {
            String id = map.get("id");
            String zoneCode = map.get("zoneCode");
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = expPayReqAuditMapper.getWaitAuditPayListByFuzzy(fuzzy,id,zoneCode);
        }
        PageInfo<ExpPayReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 拨款审核意见查询
     *
     * @param businessId 审核ID
     * @return 操作结果
     */
    @Override
    public List<FlowAuditdetail> getCurrentAuditdetailList(String businessId) {
        return flowAuditdetailMapper.getCurrentAuditdetailList(businessId, FlowTypeEnum.PAY_REQ.getType());
    }
    /**
     * 查询拨款详情
     *
     * @param id 拨款ID
     * @return 操作结果
     */
    @Override
    public ExpPayReq getPayReqInfoById(String id){
        return expPayReqAuditMapper.getPayReqInfoById(id);
    }
}
